package com.qf.shoppinguser.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.shoppinguser.entity.User;
import com.qf.shoppinguser.mapper.UserMapper;
import com.qf.shoppinguser.util.CacheManagerSevice;
import entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
// todo 22开启配置中心
@RefreshScope
public class UserController {
    @Value("${user.userToken.expireTime}")
    private String expireTime;

    @Autowired
    CacheManagerSevice cacheManagerSevice;
    @Autowired
    UserMapper userMapper;

    @GetMapping("/")
    public String get() {
        cacheManagerSevice.sSetAndTime("lx", 10, "fxy");
        return "hello"+expireTime;
    }

    @PostMapping("/login")
    public ResultData login(@RequestBody User user) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("phone", user.getPhone());
//        qw.eq("password", user.getPassword());
        User u = userMapper.selectOne(qw);
        Map<String, Object> map = new HashMap<>();
        String token = UUID.randomUUID().toString().replace("-", "");
        if (u != null) {
            if (!u.getPassword().equals(user.getPassword())) {
                return ResultData.fail("密码不正确");
            }
            map.put("user", u);
            map.put("token", token);
            cacheManagerSevice.put("user_" + token, JSONObject.toJSONString(u), Long.valueOf(expireTime));
            return new ResultData<Object>(true,map);
        } else {
            User user1 = new User(null, user.getPhone(), user.getPassword(), "淘宝用户" + System.currentTimeMillis(), "");
            userMapper.insert(user1);
            map.put("user", user1);
            map.put("token", token);
            cacheManagerSevice.put(token, JSONObject.toJSONString(user1), Long.valueOf(expireTime));
            return new ResultData<Object>(true,map);
        }
    }

    @PostMapping("/getUserByToken")
    @ResponseBody
    public ResultData getTokenByUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return ResultData.fail("请重新登录");
        }
        User user = (User) cacheManagerSevice.get("user_" + token);
        if (user == null) {
            return ResultData.fail("请重新登录");
        }
        return new ResultData<Object>(true,user);
    }

    @PostMapping("/saveInfo")
    public ResultData saveInfo(@RequestBody User user) {
        int i = userMapper.updateById(user);
        if (i > 0) {
            return new ResultData<Object>(true,"更新个人信息成功");
        } else {
            return ResultData.fail("更新个人信息失败");
        }
    }
}
